<?php

function getContentList($parent_id)
{
	if($parent_id > 0)
	{
		$i=0;
		$result = mysql_query('SELECT id, parent_id, direction, list_order, access, active FROM '.DB_PERFIX.'content_list WHERE parent_id='.$parent_id.' ORDER BY list_order ASC;');
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['parent_id'] = $row['parent_id'];
			$item[$i]['direction'] = $row['direction'];
			$item[$i]['list_order'] = $row['list_order'];
			$item[$i]['access'] = $row['access'];
			$item[$i]['active'] = $row['active'];
			$item[$i]['items'] = getContentListItems($row['id']);
			$i++;
		}
		return $item;
	}
	else
	{ return; }
}

function getContentListItems($parent_id)
{
	if($parent_id > 0)
	{
		$j=0;
		$select = 'SELECT id, parent_id, content_type, content_id, item_order, access, active FROM '.DB_PERFIX.'content_list_items WHERE parent_id='.$parent_id.' ORDER BY item_order ASC;';
		$result = mysql_query($select);
		while($row = mysql_fetch_array($result))
		{
			$subitem[$j]['id'] = $row['id'];
			$subitem[$j]['parent_id'] = $row['parent_id'];
			$subitem[$j]['content_type'] = $row['content_type'];
			$subitem[$j]['content_id'] = $row['content_id'];
			$subitem[$j]['item_order'] = $row['item_order'];
			$subitem[$j]['access'] = $row['access'];
			$subitem[$j]['active'] = $row['active'];
			$j++;
		}
		return $subitem;
	}
}
function addContentTemplate($name, $site)
{		
	if($site > 0)
	{
		$insert  = "INSERT INTO ".DB_PERFIX."content (`id`, `site_id`, `title`)"; 
		$insert .= "VALUES (NULL, ".$site.", '".$name."');";
		$result = mysql_query($insert);
		
		$result = mysql_query('SELECT id FROM '.DB_PERFIX.'content ORDER BY id DESC LIMIT 1;');
		while($row = mysql_fetch_array($result))
		{
			$item = $row['id'];
		}
	}
	return $item;
}
function updateContentTemplate($id, $title, $access, $site_area)
{
	$update  = 'UPDATE '.DB_PERFIX.'content SET ';
	$update .= 'site_area='.$site_area.', ';
	$update .= 'title="'.$title.'", ';
	$update .= 'access='.$access.' ';
	$update .= 'WHERE id='.$id.' LIMIT 1;';
	$update = mysql_query($update);
}
function updateList($id, $direction, $access)
{
	$update  = 'UPDATE '.DB_PERFIX.'content_list SET ';
	$update .= 'direction='.$direction.', ';
	$update .= 'access='.$access.' ';
	$update .= 'WHERE id='.$id.' LIMIT 1;';
	$update = mysql_query($update);
	
	$update  = 'UPDATE '.DB_PERFIX.'content_list_items SET ';
	$update .= 'access='.$access.' ';
	$update .= 'WHERE parent_id='.$id.' AND access<'.$access.';';
	$update = mysql_query($update);
}
function setListActive($id, $active)
{
	$update  = 'UPDATE '.DB_PERFIX.'content_list SET ';
	$update .= 'active='.$active.' ';
	$update .= 'WHERE id='.$id.' LIMIT 1;';
	$update = mysql_query($update);
}
function setListItemActive($id, $active)
{
	$update  = 'UPDATE '.DB_PERFIX.'content_list_items SET ';
	$update .= 'active='.$active.' ';
	$update .= 'WHERE id='.$id.' LIMIT 1;';
	
	$update = mysql_query($update);
}

function getContentTemplate($id)
{
	$result = mysql_query('SELECT id, site_id, title, access, active FROM '.DB_PERFIX.'content WHERE id='.$id.' LIMIT 1;');
	while($row = mysql_fetch_array($result))
	{
		$item['id'] = $row['id'];
		$item['site_id'] = $row['site_id'];
		$item['title'] = $row['title'];
		$item['access'] = $row['access'];
		$item['active'] = $row['active'];
	}
	return $item;
}

function setContentActive($id, $active)
{
	$update  = 'UPDATE '.DB_PERFIX.'content SET ';
	$update .= 'active='.$active.' ';
	$update .= 'WHERE id='.$id.' LIMIT 1;';
	$update = mysql_query($update);
}
function deleteContent($id)
{
	deleteContentList(0, $id);
	$result = mysql_query('DELETE FROM '.DB_PERFIX.'content WHERE id='.$id.';');
	return $result;
}
function deleteContentList($id=0, $parent_id=0)
{
	if($parent_id > 0)
	{
		$lists = getContentList($parent_id);
		if($lists)
		{
			foreach($lists as $list)
			{
				deleteContentListItems(0, $list['id']);
			}
		}
		$result = mysql_query('DELETE FROM '.DB_PERFIX.'content_list WHERE parent_id='.$parent_id.';');
	}
	else
	{
		deleteContentListItems(0, $id);
		$result = mysql_query('DELETE FROM '.DB_PERFIX.'content_list WHERE id='.$id.';');
	}
	return $result;
}
function deleteContentListItems($id=0, $parent_id=0)
{
	if($parent_id > 0)
	{
		$result = mysql_query('DELETE FROM '.DB_PERFIX.'content_list_items WHERE parent_id='.$parent_id.';');
	}
	else
	{
		$result = mysql_query('DELETE FROM '.DB_PERFIX.'content_list_items WHERE id='.$id.';');
	}
	return $result;
}

function addTemplateList($id)
{
	$neworder['list_order'] = 0;
	$result = mysql_query('SELECT list_order FROM '.DB_PERFIX.'content_list WHERE parent_id='.$id.' ORDER BY list_order DESC LIMIT 1;');
	if($result)
	{
		while($row = mysql_fetch_array($result))
		{ $neworder['list_order'] = $row['list_order']; }
	}
	$neworder['list_order'] = $neworder['list_order'] + 1;
	$insert = 'INSERT INTO '.DB_PERFIX.'content_list (`parent_id`, `list_order`) VALUES ('.$id.', '.$neworder['list_order'].');';
	$result = mysql_query($insert);
	
	return $result;
}
function deleteTemplateRow($id)
{
	$result = mysql_query('DELETE FROM '.DB_PERFIX.'content_list_items WHERE row_id='.$id.';');
	$result = mysql_query('DELETE FROM '.DB_PERFIX.'content_list WHERE id='.$id.';');
	return $result;
}
function moveListUp($id)
{
	$select = mysql_query('SELECT list_order, parent_id FROM '.DB_PERFIX.'content_list WHERE id='.$id.';');
	while($row = mysql_fetch_array($select))
	{ 
		$up_order = $row['list_order']; 
		$parent_id = $row['parent_id']; 
	}
	
	$result = mysql_query('SELECT id, list_order FROM '.DB_PERFIX.'content_list WHERE parent_id='.$parent_id.' ORDER BY list_order ASC;');
	while($row = mysql_fetch_array($result))
	{
		$current['id'] = $row['id'];
		$current['list_order'] = $row['list_order'];
		$down_order = $current['list_order'] + 1;
		if($down_order == $up_order)
		{
			$update = mysql_query('UPDATE '.DB_PERFIX.'content_list SET list_order='.$down_order.' WHERE id='.$current['id'].' LIMIT 1;');
			$update = mysql_query('UPDATE '.DB_PERFIX.'content_list SET list_order='.$current['list_order'].' WHERE id='.$id.' LIMIT 1;');
			return;
		}
	}
}
function moveListDown($id)
{
	$select = mysql_query('SELECT list_order, parent_id FROM '.DB_PERFIX.'content_list WHERE id='.$id.';');
	while($row = mysql_fetch_array($select))
	{ 
		$up_order = $row['list_order']; 
		$parent_id = $row['parent_id']; 
	}
	
	$result = mysql_query('SELECT id, list_order FROM '.DB_PERFIX.'content_list WHERE parent_id='.$parent_id.' ORDER BY list_order ASC;');
	while($row = mysql_fetch_array($result))
	{
		$current['id'] = $row['id'];
		$current['list_order'] = $row['list_order'];
		$down_order = $current['list_order'] - 1;
		if($down_order == $up_order)
		{
			$update = mysql_query('UPDATE '.DB_PERFIX.'content_list SET list_order='.$down_order.' WHERE id='.$current['id'].' LIMIT 1;');
			$update = mysql_query('UPDATE '.DB_PERFIX.'content_list SET list_order='.$current['list_order'].' WHERE id='.$id.' LIMIT 1;');
			return;
		}
	}
}
function addListItem($id)
{	
	$neworder['item_order'] = 0;
	$result = mysql_query('SELECT item_order FROM '.DB_PERFIX.'content_list_items WHERE parent_id='.$id.' ORDER BY item_order DESC LIMIT 1;');
	if($result)
	{
		while($row = mysql_fetch_array($result))
		{ $lastorder = $row['item_order']; }
	}
	if($lastorder > 0)
	{ $neworder['item_order'] = $lastorder; }
	$neworder['item_order'] = $neworder['item_order'] + 1;
	$insert = 'INSERT INTO '.DB_PERFIX.'content_list_items (`parent_id`, `item_order`) VALUES ('.$id.', '.$neworder['item_order'].');';
	$result = mysql_query($insert);
	return $result;
}
function updateListItem($id, $content_type, $content_id=0, $access)
{
	$update  = 'UPDATE '.DB_PERFIX.'content_list_items SET ';
	$update .= 'content_type='.$content_type.', ';
	$update .= 'content_id='.$content_id.', ';
	$update .= 'access='.$access.' ';
	$update .= 'WHERE id='.$id.' LIMIT 1;';
	$update = mysql_query($update);
}
function moveListItemUp($id)
{
	$select = mysql_query('SELECT item_order, parent_id FROM '.DB_PERFIX.'content_list_items WHERE id='.$id.';');
	while($row = mysql_fetch_array($select))
	{ 
		$up_order = $row['item_order']; 
		$parent_id = $row['parent_id']; 
	}
	
	$result = mysql_query('SELECT id, item_order FROM '.DB_PERFIX.'content_list_items WHERE parent_id='.$parent_id.' ORDER BY item_order ASC;');
	while($row = mysql_fetch_array($result))
	{
		$current['id'] = $row['id'];
		$current['item_order'] = $row['item_order'];
		$down_order = $current['item_order'] + 1;
		if($down_order == $up_order)
		{
			$update = mysql_query('UPDATE '.DB_PERFIX.'content_list_items SET item_order='.$down_order.' WHERE id='.$current['id'].' LIMIT 1;');
			$update = mysql_query('UPDATE '.DB_PERFIX.'content_list_items SET item_order='.$current['item_order'].' WHERE id='.$id.' LIMIT 1;');
			return;
		}
	}
}
function moveListItemDown($id)
{
	$select = mysql_query('SELECT item_order, parent_id FROM '.DB_PERFIX.'content_list_items WHERE id='.$id.';');
	while($row = mysql_fetch_array($select))
	{ 
		$up_order = $row['item_order']; 
		$parent_id = $row['parent_id']; 
	}
	
	$result = mysql_query('SELECT id, item_order FROM '.DB_PERFIX.'content_list_items WHERE parent_id='.$parent_id.' ORDER BY item_order ASC;');
	while($row = mysql_fetch_array($result))
	{
		$current['id'] = $row['id'];
		$current['item_order'] = $row['item_order'];
		$down_order = $current['item_order'] - 1;
		if($down_order == $up_order)
		{
			$update = mysql_query('UPDATE '.DB_PERFIX.'content_list_items SET item_order='.$down_order.' WHERE id='.$current['id'].' LIMIT 1;');
			$update = mysql_query('UPDATE '.DB_PERFIX.'content_list_items SET item_order='.$current['item_order'].' WHERE id='.$id.' LIMIT 1;');
			return;
		}
	}
}
?>